package com.yimi.yinhepda.views.main.home_module;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.FuncBtnEditAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.yimi.yinhepda.db.dao.FuncBtnTableDao;
import com.yimi.yinhepda.db.table.FuncBtnTable;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.utils.ToastUtil;
import com.yimi.yinhepda.base.BaseInitActivity;
import com.yimi.yinhepda.views.main.function_module.FuncTableUtils;

import org.greenrobot.eventbus.EventBus;

import java.sql.SQLException;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: lj
 * Date: 2019/10/9 16:56
 * Description:功能编辑界面
 */
public class FunctionBtnEditActivity extends BaseInitActivity implements CommonViewHolder.onItemCommonClickListener {

    @Bind(R.id.tv_select_func)
    TextView tvSelectFunc;
    @Bind(R.id.rv_func_list)
    RecyclerView rvFuncList;

    private List<FuncBtnTable> mDataList;
    private FuncBtnEditAdapter mEditAdapter;
    private int mCount;

    @Override
    public String getPageName() {
        return "功能编辑界面";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_func_btn_edit;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

        Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<? super Void> subscriber) {
                mDataList = FuncTableUtils.getList(new FuncBtnTableDao().findAll());
                for (FuncBtnTable item : mDataList) {
                    if (item.getIsCommUse() == 1) {
                        mCount++;
                    }
                }
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Void>() {
            @Override
            public void onCompleted() {
                //设置功能背景图片
                Common.setFuncPic(mDataList, true);
                tvSelectFunc.setText(mCount + "/8");
                mEditAdapter = new FuncBtnEditAdapter(FunctionBtnEditActivity.this, mDataList, FunctionBtnEditActivity.this);
                rvFuncList.setLayoutManager(new GridLayoutManager(FunctionBtnEditActivity.this, 3));
                rvFuncList.setAdapter(mEditAdapter);
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onNext(Void aVoid) {
            }
        });

    }

    @OnClick({R.id.tv_cancle, R.id.tv_finish})
    @Override
    protected void onClickBtn(View v) {
        super.onClickBtn(v);
        switch (v.getId()) {
            case R.id.tv_cancle:
                //取消
                finish();
                break;
            case R.id.tv_finish:
                //完成
                if (mDataList != null) {
                    int count = 0;
                    for (FuncBtnTable item : mDataList) {
                        if (item.getIsCommUse() == 1) {
                            count++;
                            item.setSortId(count);
                        } else {
                            item.setIsCommUse(0);
                        }
                        try {
                            new FuncBtnTableDao().update(item);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                EventBus.getDefault().post("");
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClickListener(int position) {
        if (position == RecyclerView.NO_POSITION) {
            return;
        }
        FuncBtnTable funcBtnTable = mDataList.get(position);
        if (funcBtnTable.getIsCommUse() == 1) {
            funcBtnTable.setIsCommUse(0);
            mCount--;
        } else {
            if (mCount >= 8) {
                ToastUtil.showToast("最多选择八个！");
            } else {
                funcBtnTable.setIsCommUse(1);
                mCount++;
            }
        }
        tvSelectFunc.setText(mCount + "/8");
        mEditAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemLongClickListener(int position) {

    }
}
